# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

# Preamble: Because qt5 has:
# qt5/mkspecs/features/win32/windows.prf:10: !contains(QMAKE_DEFAULT_LIBDIRS, $$QT.core.libs):
# qt5/mkspecs/features/win32/windows.prf:10:     QMAKE_LIBS += -L$$QT.core.libs
# and:
# qtbase/qmake/generators/win32/mingw_make.cpp
# << var("QMAKE_LIBS").replace(QRegExp("(\\slib|^lib)")," -l") << ' '
# << var("QMAKE_LIBS_PRIVATE").replace(QRegExp("(\\slib|^lib)")," -l") << endl;
# (or even..):
# qtbase/qmake/generators/unix/unixmake2.cpp
# t << "LIBS          = $(SUBLIBS) " << var("QMAKE_LIBS") << " " << var("QMAKE_LIBS_PRIVATE") << endl;
# .. we end up with "-LC:/msys64/mingw64/lib" appearing before the project's
# libdirs, which means an existing libqbscore1.dll.a in C:/msys64/mingw64/lib
# will get used instead of the one we're building and you'll get link errors
# or binary incompat. issues. Really, Qt shouldn't be adding this folder as
# the linker already looks in its libdir - last - anyway.
# Fixing this in qt5/mkspecs/features/win32/windows.prf may make sense?

_realname=qbs
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=1.4.1.r2684.e31bbe7
pkgrel=1
pkgdesc='Qt Build Suite'
url='http://qt-project.org'
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=('git')
options=('docs' 'staticlibs') # '!strip' 'debug')
provides=("${MINGW_PACKAGE_PREFIX}-qbs")
conflicts=("${MINGW_PACKAGE_PREFIX}-qbs")
QBS_GIT_BRANCH=1.4
# To regenerate the patches:
# git format-patch --suffix=".master.patch" -5
source=("git://gitorious.org/qt-labs/qbs.git#branch=$QBS_GIT_BRANCH"
        "manifest"

        "0001-add-hostosinfo.h-to-installed-headers.master.patch"
        "0002-WIP-Xcode-generator.master.patch"
        "0003-add-generator-header-installs.master.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-lib.master.patch"
        "0005-setup-toolchains-Pretend-msys-is-mingw.master.patch"

        "0001-add-hostosinfo.h-to-installed-headers.1.4.patch"
        "0002-WIP-Xcode-generator.1.4.patch"
        "0003-add-generator-header-installs.1.4.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch"
        "0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch"
        "0006-add-commandechomode.h-to-installed-headers.1.4.patch"
        "0007-add-many-files-to-installed-headers.1.4.patch"
        "0008-Disable-QT_NO_CAST_FROM_ASCII-due-to-Xcode-gener.1.4.patch"
        "0009-win32-Don-t-specify-LIBS-Psapi.lib-just-psapi.1.4.patch"
        "0010-Disable-WIP-Xcode-generator.1.4.patch"

        "0001-add-hostosinfo.h-to-installed-headers.1.3.4.patch"
        "0002-WIP-Xcode-generator.1.3.4.patch"
        "0003-add-generator-header-installs.1.3.4.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-libr.1.3.4.patch"
        "0005-setup-toolchains-Pretend-msys-is-mingw.1.3.4.patch")

sha256sums=('SKIP'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            '945e0ec9b5d9957a231ac3143339cf3af9767d09b4b923c95dffa25d5672164e'
            '36f638e241737038d41dfa627ecc4de14396aa457ebe4ffd154a0f7776a3cef8'
            '806c64da4c3c8e6ca4b51affe29a02d49fc303a614f34ac08bc62c66f5420ede'
            'b6d35740104f67d241060e2eeb898dee93a1ea3bed3ff3248e29ca60a39b2360'
            '0f55b433f7390106ccb6ab3f057fdb1119eddac2af78baf292a033b402f030b3'
            '50234e277e672343640e07ee351cba431446fd74d5eb75333bd31e99429fa833'
            'ca9483940e00a43323ffb8eb6dffdc6f3a663f6368fc328da94c1f89a9ab3c4f'
            'c0fd295fef9e76c2ec53481d4f86c894f28151b87b3c419f3dce6a7a48684cc0'
            '83609121b37910adce580ad725cabd96086ef952eeb1c3f5aee6adbf16bb292b'
            'c194d3fa40d40ff40210161aaa0b73b55756c3b6ae3d53920f826e15ae8b5f7d'
            '0934208fe75e34a3086d4b145f90fc53559b654140d3f1282d3fb87df3180ede'
            '10adad688e6c3222df7264d5946ba5231cb980d620ccf4393f76d1fd97b3a23f'
            'ffddbaec699bd8e9d64ff3065e1c9905f80956b68f17faac7785c518f941b00e'
            '4f814f8e89f33b7b699685deaa4682e8949ac5302a916484d2c0eb5e6e81d55d'
            '13e908e99c9a421d19ad788d51aaadfd7f0b0a6543bd116151f166145f3983ae'
            '761d4ff8072600aed4c2cd9b7e0963fc24ddda7ba6bca9431134111432ca6e85'
            '8f6f67ad1036c48efc6604e393d9d0d4c0297391bc86fc4d110995c9c0f4d191'
            'caf5258899ae870d9295079046a699a98a577b18b471d245164456918f95fe4c'
            'e412d4cdd0c0061fdadaada82826b99ada5323bca72a29b2c9f48dabdef0df8c'
            'c126c85ba840426cc9b482119b82a0ec0bc259b9a0496b4d2fadde101bc557ef')

pkgver() {
  cd "$srcdir"/$_realname
  
  local VER_PRI=$(cat qbs_version.pri | sed -n "s/QBS_VERSION = \(.*\)/\1/p")
  printf "%s.r%s.%s" "${VER_PRI}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir"/$_realname
  if [ "${QBS_GIT_BRANCH}" = "master" ]; then
    git am "${srcdir}"/0001-add-hostosinfo.h-to-installed-headers.master.patch
    git am "${srcdir}"/0002-WIP-Xcode-generator.master.patch
    git am "${srcdir}"/0003-add-generator-header-installs.master.patch
    git am "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-lib.master.patch
    git am "${srcdir}"/0005-setup-toolchains-Pretend-msys-is-mingw.master.patch
  elif [ "${QBS_GIT_BRANCH}" = "1.4" ]; then
    git am "${srcdir}"/0001-add-hostosinfo.h-to-installed-headers.1.4.patch
    git am "${srcdir}"/0002-WIP-Xcode-generator.1.4.patch
    git am "${srcdir}"/0003-add-generator-header-installs.1.4.patch
    git am "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch
    git am "${srcdir}"/0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch
    git am "${srcdir}"/0006-add-commandechomode.h-to-installed-headers.1.4.patch
    git am "${srcdir}"/0007-add-many-files-to-installed-headers.1.4.patch
    git am "${srcdir}"/0008-Disable-QT_NO_CAST_FROM_ASCII-due-to-Xcode-gener.1.4.patch
    git am "${srcdir}"/0009-win32-Don-t-specify-LIBS-Psapi.lib-just-psapi.1.4.patch
    git am "${srcdir}"/0010-Disable-WIP-Xcode-generator.1.4.patch
  elif [ "${QBS_GIT_BRANCH}" = "1.3" ]; then
    git am "${srcdir}"/0001-add-hostosinfo.h-to-installed-headers.1.3.4.patch
    git am "${srcdir}"/0002-WIP-Xcode-generator.1.3.4.patch
    git am "${srcdir}"/0003-add-generator-header-installs.1.3.4.patch
    git am "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-libr.1.3.4.patch
    git am "${srcdir}"/0005-setup-toolchains-Pretend-msys-is-mingw.1.3.4.patch
  fi
}

build() {
local _config
if check_option "debug" "n"; then
  _config="release"
else
  _config="debug"
fi
  cd "$srcdir"/$_realname
  [ -d build-${CARCH} ] && rm -rf build-${CARCH}
  mkdir build-${CARCH}
  cd build-${CARCH}
  ${MINGW_PREFIX}/bin/qmake.exe ../qbs.pro \
    CONFIG+=${_config} \
    CONFIG+=qbs_enable_project_file_updates
  # Because xcodeprojectgenerator has a build order problem.
  make -j1
  make docs
}

package() {
  cd ${srcdir}/${_realname}/build-${CARCH}
  make INSTALL_ROOT=${pkgdir}${MINGW_PREFIX} install
  make INSTALL_ROOT=${pkgdir}${MINGW_PREFIX} install_docs

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll

  for file in qbs-setup-madde-platforms qbs-setup-qt; do
    cp ${srcdir}/manifest ${pkgdir}${MINGW_PREFIX}/bin/${file}.exe.manifest
  done
}

